<?php
/**
 * Tag标签管理
 */
namespace app\index\controller;

use app\common\controller\Homebase;
use app\common\model\News as news_model;
use app\common\model\Column as column_model;
use app\common\model\TagData;

class Tag extends Homebase
{
    public function initialize()
    {
        parent::initialize();
        $this->column_model = new column_model();
        $this->news_model   = new news_model();
        $this->tag_data     = new TagData();
    }

    /**
     * tag标签的文章列表
     * @return mixed
     */
    public function index()
    {
        $tag_id = input('param.id', 0, 'intval');

        $field = 'b.*,c.name as column_name';
        $where = ['b.status'=>news_model::STATUS_ISSUE, 'a.tag_id'=>$tag_id, 'a.type'=>TagData::TYPE_NEWS];
        $list  = $this->tag_data->field($field)
                      ->alias('a')
                      ->join('__NEWS__ b', 'b.id = a.data_id')
                      ->join('__COLUMN__ c', 'b.column_id = c.id')
                      ->where($where)
                      ->order('a.id desc')
                      ->paginate(10);

        $tag_where = ['id'=>$tag_id];
        @model('common/Tag')->where($tag_where)->setInc('num');  // 更新tag阅读数量
        
        $tag_info = model('common/Tag')->where($tag_where)->find()->toArray();

        $this->assign('tag_info', $tag_info);
        $this->assign('list', $list);
        return $this->fetch();
    }

}
